using System;
using System.Collections;
using System.Data;
using BeeldBank.Business;
using BeeldBank.Business.Collection;

namespace BeeldBank.Business.Base
{
	/// <summary>
	/// Provides database independant methods for ContactInfo.
	/// </summary>
	public class ContactInfoBase : BusinessBase
	{
		internal ContactInfoBase(System.Data.DataRow row) : base(row)
		{
			this.OnPrepareForSave += new PrepareForSaveHandler(this.PrepareForSave);
		}

		public delegate void PrepareForSaveHandler();
		public event PrepareForSaveHandler OnPrepareForSave;

		#region property bool EnforceSave
		/// This property is used to indicate if the
		/// object in the save event should save, even
		/// if the object contents (marked as stereotype NOTENFORCESAVE) is not saved
		private bool _EnforceSave=false;
		public bool EnforceSave
		{
			get
			{
				return _EnforceSave;
			}
			set
			{
				_EnforceSave = value;
			}
		
		}
		#endregion

		#region PrepareForSaveBase()
		public void PrepareForSaveBase()
		{
			if (!this.PrepareForSaveFinished)
			{
				if (OnPrepareForSave != null)
				{
					OnPrepareForSave();
				}
				this.CanLoad = false;
				// call prepareforsave on all dependant objects.
			
				this.CanLoad = true;
				this.PrepareForSaveFinished = true;
			}
		}
		#endregion

		#region PrepareForSave()
		public virtual void PrepareForSave()
		{
		}
		#endregion

		#region PreSave()
		public virtual void PreSave()
		{
		}
		#endregion
				
		#region Save()
		public void Save()
		{
			bool IsSaveNeeded=ContentChanged();
			
			if (IsDirty)
			{
				this.ModifiedBy = _luw.UserName;
				if (this.State == BusinessState.New)
				{
					this.CreatedBy = _luw.UserName;
					BeeldBank.Data.ContactInfo.Insert(this._dataRow, _luw.Transaction);
					
				}
			
				if ( ( this.State == BusinessState.Changed ) && ( IsSaveNeeded ) )
				{
					BeeldBank.Data.ContactInfo.Update(this._dataRow, _luw.Transaction);
				}
				
				if ( this.State == BusinessState.Deleted )
				{
					BeeldBank.Data.ContactInfo.Delete(this._dataRow, _luw.Transaction);
					
				}
				IsDirty = false;
			}
		}
		#endregion
		
		#region OldContactInfo
		private ContactInfo _oldContactInfo;
		public ContactInfo OldContactInfo
		{
			get
			{
				if (_oldContactInfo == null && this.State != BusinessState.New)
				{
					// Create the new object in a completely separate luw,
					// otherwise a reference the the current object is returned.
					Luw luw = new Luw(this.Luw.UserName);
					_oldContactInfo = luw.NewContactInfo(BeeldBank.Data.ContactInfo.Open(this.Id).Rows[0]);
				}
				return _oldContactInfo;
			}
		}
		#endregion
		
		#region Id
		/// <summary>
		/// 
		/// </summary>
		public int Id
		{
			get
			{
				return Convert.ToInt32(_dataRow["Id"]);
			}
			set
			{
				_dataRow["Id"] = value;
				this.IsDirty = true;
			}
		}
		public bool IdIsNull
		{
			get
			{
				return _dataRow["Id"] == Convert.DBNull;
			}
		}
		public void IdSetNull()
		{
			_dataRow["Id"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Type
		/// <summary>
		/// Het type contactinfo entry<br>Er zijn momenteel twee types
		/// </summary>
		public string Type
		{
			get
			{
				return Convert.ToString(_dataRow["Type"]);
			}
			set
			{
				_dataRow["Type"] = value;
				this.IsDirty = true;
			}
		}
		public bool TypeIsNull
		{
			get
			{
				return _dataRow["Type"] == Convert.DBNull;
			}
		}
		public void TypeSetNull()
		{
			_dataRow["Type"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Naam
		/// <summary>
		/// Naam van de contact partij.
		/// </summary>
		public string Naam
		{
			get
			{
				return Convert.ToString(_dataRow["Naam"]);
			}
			set
			{
				_dataRow["Naam"] = value;
				this.IsDirty = true;
			}
		}
		public bool NaamIsNull
		{
			get
			{
				return _dataRow["Naam"] == Convert.DBNull;
			}
		}
		public void NaamSetNull()
		{
			_dataRow["Naam"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Adres
		/// <summary>
		/// Adres van de contact-partij.
		/// </summary>
		public string Adres
		{
			get
			{
				return (_dataRow["Adres"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Adres"]);
			}
			set
			{
				_dataRow["Adres"] = value;
				this.IsDirty = true;
			}
		}
		public bool AdresIsNull
		{
			get
			{
				return _dataRow["Adres"] == Convert.DBNull;
			}
		}
		public void AdresSetNull()
		{
			_dataRow["Adres"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Postcode
		/// <summary>
		/// Postcode van de contact-partij
		/// </summary>
		public string Postcode
		{
			get
			{
				return (_dataRow["Postcode"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Postcode"]);
			}
			set
			{
				_dataRow["Postcode"] = value;
				this.IsDirty = true;
			}
		}
		public bool PostcodeIsNull
		{
			get
			{
				return _dataRow["Postcode"] == Convert.DBNull;
			}
		}
		public void PostcodeSetNull()
		{
			_dataRow["Postcode"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Plaats
		/// <summary>
		/// Plaats van de contact-partij
		/// </summary>
		public string Plaats
		{
			get
			{
				return (_dataRow["Plaats"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Plaats"]);
			}
			set
			{
				_dataRow["Plaats"] = value;
				this.IsDirty = true;
			}
		}
		public bool PlaatsIsNull
		{
			get
			{
				return _dataRow["Plaats"] == Convert.DBNull;
			}
		}
		public void PlaatsSetNull()
		{
			_dataRow["Plaats"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Telefoon
		/// <summary>
		/// Telefoon van de contact-partij
		/// </summary>
		public string Telefoon
		{
			get
			{
				return Convert.ToString(_dataRow["Telefoon"]);
			}
			set
			{
				_dataRow["Telefoon"] = value;
				this.IsDirty = true;
			}
		}
		public bool TelefoonIsNull
		{
			get
			{
				return _dataRow["Telefoon"] == Convert.DBNull;
			}
		}
		public void TelefoonSetNull()
		{
			_dataRow["Telefoon"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region TelBereikbaarheid
		/// <summary>
		/// Eventuele opmerkingen betreffende de telefonische bereikbaarheid.
		/// </summary>
		public string TelBereikbaarheid
		{
			get
			{
				return (_dataRow["TelBereikbaarheid"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["TelBereikbaarheid"]);
			}
			set
			{
				_dataRow["TelBereikbaarheid"] = value;
				this.IsDirty = true;
			}
		}
		public bool TelBereikbaarheidIsNull
		{
			get
			{
				return _dataRow["TelBereikbaarheid"] == Convert.DBNull;
			}
		}
		public void TelBereikbaarheidSetNull()
		{
			_dataRow["TelBereikbaarheid"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Fax
		/// <summary>
		/// Faxnummer van de contact-partij.
		/// </summary>
		public string Fax
		{
			get
			{
				return (_dataRow["Fax"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Fax"]);
			}
			set
			{
				_dataRow["Fax"] = value;
				this.IsDirty = true;
			}
		}
		public bool FaxIsNull
		{
			get
			{
				return _dataRow["Fax"] == Convert.DBNull;
			}
		}
		public void FaxSetNull()
		{
			_dataRow["Fax"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Email
		/// <summary>
		/// Email van de contact-partij.
		/// </summary>
		public string Email
		{
			get
			{
				return Convert.ToString(_dataRow["Email"]);
			}
			set
			{
				_dataRow["Email"] = value;
				this.IsDirty = true;
			}
		}
		public bool EmailIsNull
		{
			get
			{
				return _dataRow["Email"] == Convert.DBNull;
			}
		}
		public void EmailSetNull()
		{
			_dataRow["Email"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region ContactPersoonAanhef
		/// <summary>
		/// Aanhef van de contactpersoon
		/// </summary>
		public string ContactPersoonAanhef
		{
			get
			{
				return (_dataRow["ContactPersoonAanhef"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["ContactPersoonAanhef"]);
			}
			set
			{
				_dataRow["ContactPersoonAanhef"] = value;
				this.IsDirty = true;
			}
		}
		public bool ContactPersoonAanhefIsNull
		{
			get
			{
				return _dataRow["ContactPersoonAanhef"] == Convert.DBNull;
			}
		}
		public void ContactPersoonAanhefSetNull()
		{
			_dataRow["ContactPersoonAanhef"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region ContactPersoonVoorletters
		/// <summary>
		/// Voorletters van de contactpersoon
		/// </summary>
		public string ContactPersoonVoorletters
		{
			get
			{
				return (_dataRow["ContactPersoonVoorletters"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["ContactPersoonVoorletters"]);
			}
			set
			{
				_dataRow["ContactPersoonVoorletters"] = value;
				this.IsDirty = true;
			}
		}
		public bool ContactPersoonVoorlettersIsNull
		{
			get
			{
				return _dataRow["ContactPersoonVoorletters"] == Convert.DBNull;
			}
		}
		public void ContactPersoonVoorlettersSetNull()
		{
			_dataRow["ContactPersoonVoorletters"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region ContactPersoonAchternaam
		/// <summary>
		/// De achternaam van de contactpersoon
		/// </summary>
		public string ContactPersoonAchternaam
		{
			get
			{
				return (_dataRow["ContactPersoonAchternaam"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["ContactPersoonAchternaam"]);
			}
			set
			{
				_dataRow["ContactPersoonAchternaam"] = value;
				this.IsDirty = true;
			}
		}
		public bool ContactPersoonAchternaamIsNull
		{
			get
			{
				return _dataRow["ContactPersoonAchternaam"] == Convert.DBNull;
			}
		}
		public void ContactPersoonAchternaamSetNull()
		{
			_dataRow["ContactPersoonAchternaam"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region OrderBevestigingsEmail1
		/// <summary>
		/// Het email adres voor de orderbevestiging
		/// </summary>
		public string OrderBevestigingsEmail1
		{
			get
			{
				return (_dataRow["OrderBevestigingsEmail1"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["OrderBevestigingsEmail1"]);
			}
			set
			{
				_dataRow["OrderBevestigingsEmail1"] = value;
				this.IsDirty = true;
			}
		}
		public bool OrderBevestigingsEmail1IsNull
		{
			get
			{
				return _dataRow["OrderBevestigingsEmail1"] == Convert.DBNull;
			}
		}
		public void OrderBevestigingsEmail1SetNull()
		{
			_dataRow["OrderBevestigingsEmail1"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region OrderBevestigingsEmail2
		/// <summary>
		/// Het tweede email adres voor de orderbevestiging.
		/// </summary>
		public string OrderBevestigingsEmail2
		{
			get
			{
				return (_dataRow["OrderBevestigingsEmail2"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["OrderBevestigingsEmail2"]);
			}
			set
			{
				_dataRow["OrderBevestigingsEmail2"] = value;
				this.IsDirty = true;
			}
		}
		public bool OrderBevestigingsEmail2IsNull
		{
			get
			{
				return _dataRow["OrderBevestigingsEmail2"] == Convert.DBNull;
			}
		}
		public void OrderBevestigingsEmail2SetNull()
		{
			_dataRow["OrderBevestigingsEmail2"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region CreatedBy
		public String CreatedBy
		{
			get
			{
				return Convert.ToString(_dataRow["CreatedBy"]);
			}
			set
			{
				_dataRow["CreatedBy"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region CreatedAt
		public DateTime CreatedAt
		{
			get
			{
				return Convert.ToDateTime(_dataRow["CreatedAt"]);
			}
			set
			{
				_dataRow["CreatedAt"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ModifiedBy
		public String ModifiedBy
		{
			get
			{
				return Convert.ToString(_dataRow["ModifiedBy"]);
			}
			set
			{
				_dataRow["ModifiedBy"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ModifiedAt
		public DateTime ModifiedAt
		{
			get
			{
				return Convert.ToDateTime(_dataRow["ModifiedAt"]);
			}
			set
			{
				_dataRow["ModifiedAt"] = value;
				this.IsDirty = true;
			}
		}
		#endregion
	
		#region Version
		public int Version
		{
			get
			{
				return _dataRow["Version"] == Convert.DBNull ? -1 : Convert.ToInt32(_dataRow["Version"]);
			}
			set
			{
				_dataRow["Version"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ContentChanged
		/// <summary>
		/// ContentChanged
		/// This function checks whether the content of a record is changed, dependant on the "EnforceSave" 
		/// variable. If EnforceSave is false, all the fields which have not been marked "NOTENFORCINGSAVE" as
		/// a stereotype in the design are checked. The contents of the fields are checked against the old values
		/// if they are not equal,the result is true. If the contents are equal, the result is false.
		/// 
		/// If EnforceSave is true, the result is always true.
		/// </summary>
		/// <returns>boolean indicating whether the Content is Changed</returns>
		public bool ContentChanged()
		{
			// Change enforced by Class Variables
			if( this.EnforceSave ) { return( true ); }
			if( this.IdIsNull) { return( true ); }
			
			// Change enforced by the attributes
			
			#region Id
			/// Id is excluded because it is the primary key
			#endregion
					
			#region Type
			
			if( this.TypeIsNull != this.OldContactInfo.TypeIsNull ) { return( true ); }
			if( this.Type != this.OldContactInfo.Type) { return( true ); }
	
			#endregion
					
			#region Naam
			
			if( this.NaamIsNull != this.OldContactInfo.NaamIsNull ) { return( true ); }
			if( this.Naam != this.OldContactInfo.Naam) { return( true ); }
	
			#endregion
					
			#region Adres
			
			if( this.AdresIsNull != this.OldContactInfo.AdresIsNull ) { return( true ); }
			if( this.Adres != this.OldContactInfo.Adres) { return( true ); }
	
			#endregion
					
			#region Postcode
			
			if( this.PostcodeIsNull != this.OldContactInfo.PostcodeIsNull ) { return( true ); }
			if( this.Postcode != this.OldContactInfo.Postcode) { return( true ); }
	
			#endregion
					
			#region Plaats
			
			if( this.PlaatsIsNull != this.OldContactInfo.PlaatsIsNull ) { return( true ); }
			if( this.Plaats != this.OldContactInfo.Plaats) { return( true ); }
	
			#endregion
					
			#region Telefoon
			
			if( this.TelefoonIsNull != this.OldContactInfo.TelefoonIsNull ) { return( true ); }
			if( this.Telefoon != this.OldContactInfo.Telefoon) { return( true ); }
	
			#endregion
					
			#region TelBereikbaarheid
			
			if( this.TelBereikbaarheidIsNull != this.OldContactInfo.TelBereikbaarheidIsNull ) { return( true ); }
			if( this.TelBereikbaarheid != this.OldContactInfo.TelBereikbaarheid) { return( true ); }
	
			#endregion
					
			#region Fax
			
			if( this.FaxIsNull != this.OldContactInfo.FaxIsNull ) { return( true ); }
			if( this.Fax != this.OldContactInfo.Fax) { return( true ); }
	
			#endregion
					
			#region Email
			
			if( this.EmailIsNull != this.OldContactInfo.EmailIsNull ) { return( true ); }
			if( this.Email != this.OldContactInfo.Email) { return( true ); }
	
			#endregion
					
			#region ContactPersoonAanhef
			
			if( this.ContactPersoonAanhefIsNull != this.OldContactInfo.ContactPersoonAanhefIsNull ) { return( true ); }
			if( this.ContactPersoonAanhef != this.OldContactInfo.ContactPersoonAanhef) { return( true ); }
	
			#endregion
					
			#region ContactPersoonVoorletters
			
			if( this.ContactPersoonVoorlettersIsNull != this.OldContactInfo.ContactPersoonVoorlettersIsNull ) { return( true ); }
			if( this.ContactPersoonVoorletters != this.OldContactInfo.ContactPersoonVoorletters) { return( true ); }
	
			#endregion
					
			#region ContactPersoonAchternaam
			
			if( this.ContactPersoonAchternaamIsNull != this.OldContactInfo.ContactPersoonAchternaamIsNull ) { return( true ); }
			if( this.ContactPersoonAchternaam != this.OldContactInfo.ContactPersoonAchternaam) { return( true ); }
	
			#endregion
					
			#region OrderBevestigingsEmail1
			
			if( this.OrderBevestigingsEmail1IsNull != this.OldContactInfo.OrderBevestigingsEmail1IsNull ) { return( true ); }
			if( this.OrderBevestigingsEmail1 != this.OldContactInfo.OrderBevestigingsEmail1) { return( true ); }
	
			#endregion
					
			#region OrderBevestigingsEmail2
			
			if( this.OrderBevestigingsEmail2IsNull != this.OldContactInfo.OrderBevestigingsEmail2IsNull ) { return( true ); }
			if( this.OrderBevestigingsEmail2 != this.OldContactInfo.OrderBevestigingsEmail2) { return( true ); }
	
			#endregion
							
			
			return false;
		}
		#endregion	

		#region WhereAmI
		/// <summary>
		/// Provides the WhereAmI display text for a ContactInfo with the specified id
		/// </summary>
		/// <param name="id">Id of the ContactInfo.</param>
		/// <return>>WhereAmI display text</returns>
		public static string WhereAmI(int id)
		{
			string whereAmI = "";
			if (id == 0)
			{
				whereAmI = "New ContactInfo";
			}
			else
			{
				DataTable table = BeeldBank.Data.ContactInfo.Show(id);
				
				whereAmI = whereAmI + table.Rows[0]["Naam"].ToString();
				
			}
			return whereAmI;
		}
		#endregion
	}
}
